"use strict";var widgetoptsSettingsModule={init:function(){jQuery(".widgetopts-module-settings-container").hide(),this.bindEvents()},bindEvents:function(){var e=jQuery("#wpcontent");e.on("click",".widgetopts-toggle-settings, .widgetopts-module-settings-cancel",this.openModal),e.on("click",".widgetopts-close-modal, .widgetopts-modal-background",this.closeModal),e.on("keyup",this.closeModal),e.on("click",".widgetopts-toggle-activation",this.moduleToggle),e.on("click",".widgetopts-module-settings-save",this.saveSettings),e.on("click",".opts-add-class-btn",this.toggleCustomClass),e.on("click",".opts-remove-class-btn",this.removeCustomClass),e.on("click",".widgetopts-delete-cache",this.clearWidgetCache),e.on("click",".widgetopts-license_deactivate",this.deactivationHandler)},openModal:function(e){e.preventDefault();var t=jQuery(this).parents(".widgetopts-module-card").find(".widgetopts-module-settings-container");jQuery(".widgetopts-modal-background").show(),t.show(),jQuery("body").addClass("widgetopts-modal-open")},closeModal:function(e){void 0!==e&&(e.preventDefault(),"keyup"===e.type&&27!==e.which)||(jQuery(".widgetopts-modal-background").hide(),jQuery(".widgetopts-module-settings-container").hide(),jQuery("body").removeClass("widgetopts-modal-open"))},moduleToggle:function(e){e.preventDefault(),e.stopPropagation();var t=jQuery(this),s=t.parents(".widgetopts-module-card"),a=s.find(".widgetopts-toggle-activation"),o=s.attr("data-module-id");if(a.prop("disabled",!0),t.html()==widgetopts.translation.activate)var i="activate";else var i="deactivate";widgetoptsSettingsModule.ajaxRequest(o,i,{},widgetoptsSettingsModule.moduleCallback)},moduleCallback:function(e){var t=e.module,s=e.method,a=jQuery("#widgetopts-module-card-"+t),o=a.find(".widgetopts-toggle-activation");if(e.errors.length>0)return o.html(widgetopts.translations.error).addClass("button-secondary").removeClass("button-primary"),void setTimeout(function(){widgetoptsSettingsModule.isModuleActive(t)},1e3);if("activate"===s){if(o.html(widgetopts.translation.deactivate).addClass("button-secondary").removeClass("button-primary").prop("disabled",!1),a.addClass("widgetopts-module-type-enabled").removeClass("widgetopts-module-type-disabled"),a.hasClass("no-settings"))var i=widgetopts.translation.show_description;else var i=widgetopts.translation.show_settings}else{o.html(widgetopts.translation.activate).addClass("button-primary").removeClass("button-secondary").prop("disabled",!1),a.addClass("widgetopts-module-type-disabled").removeClass("widgetopts-module-type-enabled");var i=widgetopts.translation.show_description}a.find(".widgetopts-toggle-settings").html(i)},saveSettings:function(e){e.preventDefault();var t=jQuery(this);if(t.hasClass("widgetopts-module-settings-save"))var s=t.parents(".widgetopts-module-card").attr("data-module-id");else var s="";t.prop("disabled",!0);var a={"--widgetopts-form-serialized-data":jQuery("#widgetopts-module-settings-form").serialize()};widgetoptsSettingsModule.ajaxRequest(s,"save",a,widgetoptsSettingsModule.savingCallback)},savingCallback:function(e){""===e.module?jQuery("#widgetopts-save").prop("disabled",!1):jQuery("#widgetopts-module-card-"+e.module+" button.widgetopts-module-settings-save").prop("disabled",!1);var t=jQuery(".widgetopts-module-cards-container");widgetoptsSettingsModule.clearMessages(),e.errors.length>0||!e.closeModal?(widgetoptsSettingsModule.showMessages(e.messages,e.module,"open"),t.find(".widgetopts-module-settings-content-container:visible").animate({scrollTop:0},"fast")):(widgetoptsSettingsModule.showMessages(e.messages,e.module,"closed"),t.find(".widgetopts-module-settings-content-container:visible").scrollTop(0),widgetoptsSettingsModule.closeModal())},clearMessages:function(){jQuery("#widgetopts-settings-messages-container, .widgetopts-module-messages-container").empty()},showMessages:function(e,t,s){jQuery.each(e,function(e,a){widgetoptsSettingsModule.showMessage(a,t,s)})},showMessage:function(e,t,s){if("closed"!==s&&"open"!==s&&(s="closed"),"string"!=typeof t&&(t=""),"closed"===s||""===t){var a=jQuery("#widgetopts-settings-messages-container");setTimeout(function(){a.removeClass("visible"),setTimeout(function(){a.find("div").remove()},500)},4e3)}else var a=jQuery("#widgetopts-module-card-"+t+" .widgetopts-module-messages-container");a.append('<div class="updated fade"><p><strong>'+e+"</strong></p></div>").addClass("visible")},ajaxRequest:function(e,t,s,a){var o={action:widgetopts.ajax_action,nonce:widgetopts.ajax_nonce,module:e,method:t,data:s};jQuery.post(ajaxurl,o).always(function(o,i,d){widgetoptsSettingsModule.processAjaxResponse(o,i,d,e,t,s,a)})},processAjaxResponse:function(e,t,s,a,o,i,d){var n={module:a,method:o,data:i,status:t,license_status:null,jqxhr:null,success:!1,response:null,button:null,errors:[],messages:[],functionCalls:[],redirect:!1,closeModal:!0};"WIDGETOPTS_Response"===(e=jQuery.parseJSON(e)).source&&"undefined"!==e.response&&(n.jqxhr=s,n.success=e.success,n.response=e.response,n.errors=e.errors,n.messages=e.messages,n.functionCalls=e.functionCalls,n.redirect=e.redirect,n.closeModal=e.closeModal,n.button=e.button,void 0!==n.license_status&&(n.license_status=e.license_status)),"function"==typeof d?d(n):"function"==typeof console.log&&console.log("ERROR: Unable to handle settings AJAX request due to an invalid callback:",d,{data:postData,results:n})},toggleCustomClass:function(e){var t=jQuery(".opts-add-class-txtfld").val(),s="extwopts_class_settings[classlists][]";jQuery(this).hasClass("widgetopts-add-class-btn")&&(s="classes[classlists][]"),t.length>0&&(jQuery("#opts-predefined-classes ul").append('<li><input type="hidden" name="'+s+'" value="'+t+'" /><span class"opts-li-value">'+t+'</span> <a href="#" class="opts-remove-class-btn"><span class="dashicons dashicons-dismiss"></span></a></li>'),jQuery(".opts-add-class-txtfld").val("")),e.preventDefault(),e.stopPropagation()},removeCustomClass:function(e){jQuery(this).parent("li").fadeOut("fast",function(){jQuery(this).remove()}),e.preventDefault(),e.stopPropagation()},clearWidgetCache:function(e){return jQuery(this).prop("disabled",!0),widgetoptsSettingsModule.ajaxRequest("clear_cache","clear_cache","",widgetoptsSettingsModule.clearWidgetCacheCallback),!1},clearWidgetCacheCallback:function(e){void 0!==e.response&&(jQuery(".widgetopts-delete-cache").after('<span class="dashicons dashicons-yes widgetopts-cache-dashicons"></span>'),jQuery(".widgetopts-cache-dashicons").delay(2e3).fadeOut(400),jQuery(".widgetopts-delete-cache").prop("disabled",!1))},deactivationHandler:function(e){e.preventDefault();var t,s=jQuery(this);if(s.prop("disabled",!0),""!=(t=jQuery("#"+s.attr("data-target"))).val()){var a={"license-data":t.val(),"license-action":"deactivate",shortname:s.attr("data-shortname"),button:s.attr("id")};widgetoptsSettingsModule.ajaxRequest("license_key","deactivate_license",a,widgetoptsSettingsModule.licenseDeactivationCallback)}else t.css({border:"1px solid red"}),s.prop("disabled",!1)},licenseDeactivationCallback:function(e){if(void 0!==e.response&&void 0!==e.messages&&void 0!==e.button){var t=jQuery("#"+e.button);jQuery("#"+t.attr("data-target")).before("<span>"+e.messages[0]+"</span>"),"deactivated"==e.success&&(t.parent("td").parent("tr").fadeOut(),jQuery("#"+t.attr("data-target")).val(""))}t.prop("disabled",!1)}};jQuery(document).ready(function(){widgetoptsSettingsModule.init()});